package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 1. 构造购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 2. 查询user_id
        Long user_id = BaseContext.getCurrentId();
        shoppingCart.setUserId(user_id);
        // 3. 查询购物车是否存在
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.listShoppingCart(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            // 4. 存在，更新数据
            ShoppingCart cart = shoppingCartList.get(0);
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        } else {
            // 5. 不存在，添加数据
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            // 判断添加的是什么
            if (dishId != null) {
                // 添加的是菜品
                Dish dish = dishMapper.selectById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());

                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
            } else if (setmealId != null) {
                // 添加的是套餐
                Setmeal setmeal = setmealMapper.selectById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());

                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
            }
            // 6. 插入数据
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.listShoppingCart(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 减少购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        // 1. 构造购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 2. 查询user_id
        Long user_id = BaseContext.getCurrentId();
        shoppingCart.setUserId(user_id);
        // 3. 查询购物车
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.listShoppingCart(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            // 4. 存在，更新数据
            ShoppingCart cart = shoppingCartList.get(0);
            if (cart.getNumber() > 1) {
                // 5. 数量大于1，减少数量
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.update(cart);
            } else {
                // 6. 数量等于1，删除购物车
                shoppingCartMapper.delete(cart.getId());
            }
        }
    }

    /**
     * 清空购物车
     * @return
     */
    @Override
    public void clearShoppinCart() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }
}
